package history

import (
	"fmt"
	"math/rand"
)

/**
 * @Description: 有序二维数组，顺时针打印
 * @Keyword:
 * @Author: kami
 * @Date: 2022/8/11 16:10
 **/
func printArrayCycle(nums [][]int) []int {
	var (
		res    []int
		top    = 0
		bottom = len(nums) - 1
		left   = 0
		right  = len(nums[0]) - 1
		cnt    = len(nums) * len(nums[0])
		step   = 0
	)
	for step < cnt {
		// 右
		for i := left; i <= right; i++ {
			res = append(res, nums[top][i])
			step++
		}
		if step >= cnt {
			return res
		}
		top++
		// 下
		for i := top; i <= bottom; i++ {
			res = append(res, nums[i][right])
			step++
		}
		if step >= cnt {
			return res
		}
		right--
		// 左
		for i := right; i >= left; i-- {
			res = append(res, nums[bottom][i])
			step++
		}
		if step >= cnt {
			return res
		}
		bottom--
		// 上
		for i := bottom; i >= top; i-- {
			res = append(res, nums[i][left])
			step++
		}
		if step >= cnt {
			return res
		}
		left++
	}
	return res

}

func randStr(strs []string, power []int) {
	if len(strs) != len(power) {
		return
	}
	var sum int
	for i := 0; i < len(power); i++ {
		sum += power[i]
	}
	var rands = make([]int, sum)
	var idx int
	for i := 0; i < len(power); i++ {
		for j := 0; j < power[i]; j++ {
			rands[idx] = i
			idx++
		}
	}

	for i := 0; i < 10; i++ {
		var randInd = rand.Intn(sum)
		fmt.Println(strs[rands[randInd]])
	}
}

func randStrBanerry(strs []string, power []int) {
	if len(strs) != len(power) {
		return
	}
	var sum int
	var rands = make([]int, sum)
	for i := 0; i < len(power); i++ {
		sum += power[i]
		rands[i] = sum
	}

	// 1,4,7,10
	var idxPrint int
	for i := 0; i < 10; i++ {
		var randInd = rand.Intn(sum)
		var left int
		var right = len(rands) - 1
		for left < right {
			var mid = (left + right) / 2
			if rands[mid] >= randInd && randInd < rands[mid+1] {
				idxPrint = mid + 1
			} else if rands[mid] >= randInd {
				left = mid + 1
			} else {
				right = mid - 1
			}
		}

		fmt.Println(strs[rands[idxPrint]])
	}
}

/**
 * @description: 数组 3个元素，字符，flow，flower，flowe找最大前缀字符
 * @keyword:
 * @author: kami
 * @date 2024/4/16 15:08
 */
func findMaxPrefix(strs []string) string {
	if len(strs) == 0 {
		return ""
	}
	var maxIdx = len(strs[0]) - 1
	for i := 1; i < len(strs); i++ {
		if len(strs[i])-1 < maxIdx {
			maxIdx = len(strs[i]) - 1
		}
		var curIdx = 0
		for curIdx <= maxIdx {
			if strs[i][curIdx] != strs[0][curIdx] {
				maxIdx = curIdx - 1
				break
			}
			curIdx++
		}
	}
	if maxIdx <= 0 {
		return ""
	}
	return strs[0][:maxIdx+1]
}
